﻿using System;
using System.Collections.Generic;
using System.Linq;
using DTO;
namespace DAO
{
    public class TinRaoVatDAO
    {
        public static List<TinRaoVatDTO> getAlongCode(int maDanhMucCon)
        {
            List<TinRaoVatDTO> result = new List<TinRaoVatDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaDanhMucCon == maDanhMucCon).OrderByDescending(dm => dm.ThoiGianUp).ToList();

            foreach (var ds in query)
            {
                result.Add(new TinRaoVatDTO(ds.MaTinRaoVat, ds.TenTinRaoVat, ds.Thumbnail, int.Parse(ds.GiaTien.ToString()), ds.NoiDungTinRaoVat, DateTime.Parse(ds.ThoiGianDang.ToString())
            , int.Parse(ds.SoLanXem.ToString()), int.Parse(ds.MaDiaDiem.ToString()), int.Parse(ds.MaNguoiDung.ToString()),
            int.Parse(ds.MaDanhMucCon.ToString()), int.Parse(ds.MaNgonNgu.ToString()), DateTime.Parse(ds.ThoiGianUp.ToString()), 0));
            }

            return result;
        }

        public static List<TinRaoVatDTO> getAll()
        {
            List<TinRaoVatDTO> result = new List<TinRaoVatDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINRAOVATs.Where(dm => dm.Deleted.Equals(0)
                             ).OrderByDescending(dm => dm.ThoiGianUp).ToList();

            foreach (var ds in query)
            {
                result.Add(new TinRaoVatDTO(ds.MaTinRaoVat, ds.TenTinRaoVat, ds.Thumbnail, int.Parse(ds.GiaTien.ToString()), ds.NoiDungTinRaoVat, DateTime.Parse(ds.ThoiGianDang.ToString())
            , int.Parse(ds.SoLanXem.ToString()), int.Parse(ds.MaDiaDiem.ToString()), int.Parse(ds.MaNguoiDung.ToString()),
            int.Parse(ds.MaDanhMucCon.ToString()), int.Parse(ds.MaNgonNgu.ToString()), DateTime.Parse(ds.ThoiGianUp.ToString()), 0));
            }

            return result;
        }

        public static TinRaoVatDTO getAlongCode_MaTinRaoVat(int maTin)
        {
            TinRaoVatDTO result = new TinRaoVatDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaTinRaoVat == maTin).Single();

            result.MaTinRaoVat = query.MaTinRaoVat;
            result.TenTinRaoVat = query.TenTinRaoVat;
            result.Thumbnail = query.Thumbnail;
            result.GiaTien = query.GiaTien.Value;
            result.NoiDungTinRaoVat = query.NoiDungTinRaoVat;
            result.ThoiGianDang = query.ThoiGianDang.Value;
            result.SoLanXem = query.SoLanXem.Value;
            result.MaDiaDiem = query.MaDiaDiem.Value;
            result.MaNguoiDung = query.MaNguoiDung.Value;
            result.MaDanhMucCon = query.MaDanhMucCon.Value;
            result.MaNgonNgu = query.MaNgonNgu.Value;
            result.ThoiGianUp = query.ThoiGianUp.Value;

            return result;
        }

        public static TinRaoVatDTO LayTinRaoVatTheoTen_NoiDung_ThoiGianDang(string ten, string noidung, DateTime thoigiandang)
        {
            TinRaoVatDTO result = new TinRaoVatDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TINRAOVATs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.TenTinRaoVat == ten &&
                             dm.NoiDungTinRaoVat == noidung &&
                             dm.ThoiGianDang == thoigiandang).First();

            result.MaTinRaoVat = query.MaTinRaoVat;
            result.TenTinRaoVat = query.TenTinRaoVat;
            result.Thumbnail = query.Thumbnail;
            result.GiaTien = query.GiaTien.Value;
            result.NoiDungTinRaoVat = query.NoiDungTinRaoVat;
            result.ThoiGianDang = query.ThoiGianDang.Value;
            result.SoLanXem = query.SoLanXem.Value;
            result.MaDiaDiem = query.MaDiaDiem.Value;
            result.MaNguoiDung = query.MaNguoiDung.Value;
            result.MaDanhMucCon = query.MaDanhMucCon.Value;
            result.MaNgonNgu = query.MaNgonNgu.Value;
            result.ThoiGianUp = query.ThoiGianUp.Value;

            return result;
        }

        public static void Insert(TinRaoVatDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINRAOVAT tin = new TINRAOVAT();
            tin.TenTinRaoVat = _insert.TenTinRaoVat;
            tin.Thumbnail = _insert.Thumbnail;
            tin.GiaTien = _insert.GiaTien;
            tin.NoiDungTinRaoVat = _insert.NoiDungTinRaoVat;
            tin.ThoiGianDang = _insert.ThoiGianDang;
            tin.SoLanXem = _insert.SoLanXem;
            tin.MaDiaDiem = _insert.MaDiaDiem;
            tin.MaNguoiDung = _insert.MaNguoiDung;
            tin.MaDanhMucCon = _insert.MaDanhMucCon;
            tin.MaNgonNgu = _insert.MaNgonNgu;
            tin.Deleted = _insert.Deleted;
            tin.ThoiGianUp = _insert.ThoiGianUp;

            dbRaoVat.TINRAOVATs.InsertOnSubmit(tin);
            dbRaoVat.SubmitChanges();
        }

        public static bool Update(TinRaoVatDTO _trv)
        {
            bool result = true;
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINRAOVAT _update = (from n in dbRaoVat.TINRAOVATs
                                 where n.MaTinRaoVat == _trv.MaTinRaoVat
                                 select n).Single();

            _update.TenTinRaoVat = _trv.TenTinRaoVat;
            _update.Deleted = _trv.Deleted;
            _update.GiaTien = _trv.GiaTien;
            _update.MaDanhMucCon = _trv.MaDanhMucCon;
            if (_trv.Thumbnail != null)
                _update.Thumbnail = _trv.Thumbnail;
            _update.MaDiaDiem = _trv.MaDiaDiem;
            _update.MaNgonNgu = _trv.MaNgonNgu;
            _update.MaNguoiDung = _trv.MaNguoiDung;
            _update.SoLanXem = _trv.SoLanXem;
            _update.ThoiGianDang = _trv.ThoiGianDang;
            _update.ThoiGianUp = _trv.ThoiGianUp;

            try
            {
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        public static bool Delete(int maRaoVat)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINRAOVAT _tinraovat = (from n in dbRaoVat.TINRAOVATs
                                    where n.MaTinRaoVat == maRaoVat && n.Deleted == 0
                                    select n).Single();

            _tinraovat.Deleted = 1;
            try
            {
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public static List<TinRaoVatDTO> Search(string chuoi)
        {
            List<TinRaoVatDTO> Result = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> Temp = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> DS = getAll();

            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].TenTinRaoVat.ToLower().Contains(chuoi.ToLower()))
                {
                    Result.Add(DS[i]);
                    Temp.Add(DS[i]);
                }
            }

            for (int j = 0; j < DS.Count; ++j)
            {
                if (DS[j].NoiDungTinRaoVat.ToLower().Contains(chuoi.ToLower()))
                {
                    if (KiemTraTonTai(Temp, DS[j]) == false)
                    {
                        Result.Add(DS[j]);
                    }
                }
            }

            return Result;
        }

        private static TinRaoVatDTO ChuyenDTO(TINRAOVAT tinraovat)
        {
            TinRaoVatDTO ketQua = new TinRaoVatDTO();

            ketQua.MaTinRaoVat = tinraovat.MaTinRaoVat;
            ketQua.TenTinRaoVat = tinraovat.TenTinRaoVat;
            ketQua.Thumbnail = tinraovat.Thumbnail;
            ketQua.GiaTien = tinraovat.GiaTien.Value;
            ketQua.NoiDungTinRaoVat = tinraovat.NoiDungTinRaoVat;
            ketQua.ThoiGianDang = tinraovat.ThoiGianDang.Value;
            ketQua.SoLanXem = tinraovat.SoLanXem.Value;
            ketQua.MaDiaDiem = tinraovat.MaDiaDiem.Value;
            ketQua.MaNguoiDung = tinraovat.MaNguoiDung.Value;
            ketQua.MaDanhMucCon = tinraovat.MaDanhMucCon.Value;
            ketQua.MaNgonNgu = tinraovat.MaNgonNgu.Value;
            ketQua.ThoiGianUp = tinraovat.ThoiGianUp.Value;
            ketQua.Deleted = tinraovat.Deleted.Value;

            return ketQua;
        }

        public static List<TinRaoVatDTO> SearchNangCao(string ten, string noidung,
            string danhmuc, string diadiem, int giatu, int giaden, DateTime thoigiantu, DateTime thoigianden)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            List<TinRaoVatDTO> ketQua = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> ketQua1 = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> ketQua2 = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> ketQua3 = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> ketQua4 = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> ketQua5 = new List<TinRaoVatDTO>();
            List<TinRaoVatDTO> ketQua6 = new List<TinRaoVatDTO>();

            int SoTieuChiTraCuu = 0;//Dùng đếm số lương tiêu chí tra cứu.
            int dem = 0;            //Dùng để so sánh đối tượng tra cứu được trong 
            //tiêu chí đầu tiên có xuất hiện trong tất cả tiêu chí còn lại.
            int count = 0;

            if (ten.Length > 0)
            {
                SoTieuChiTraCuu++;
                var query = dbRaoVat.TINRAOVATs.Where(hs => hs.TenTinRaoVat.ToLower().Contains(ten) &&
                    hs.Deleted == 0);

                foreach (TINRAOVAT trv in query)
                {
                    ketQua1.Add(ChuyenDTO(trv));
                    ketQua.Add(ChuyenDTO(trv));
                }

                if (count == 0)
                {
                    count = ketQua1.Count;
                }
            }

            if (noidung.Length > 0)
            {
                SoTieuChiTraCuu++;
                var query = dbRaoVat.TINRAOVATs.Where(hs => hs.NoiDungTinRaoVat.ToLower().Contains(noidung) &&
                    hs.Deleted == 0);

                foreach (TINRAOVAT trv in query)
                {
                    ketQua2.Add(ChuyenDTO(trv));
                    ketQua.Add(ChuyenDTO(trv));
                }

                if (count == 0)
                {
                    count = ketQua2.Count;
                }
            }

            if (danhmuc.CompareTo("......") != 0)
            {
                int maDanhMuc = DanhMucConDAO.getMa(danhmuc);

                SoTieuChiTraCuu++;
                var query = dbRaoVat.TINRAOVATs.Where(hs => hs.MaDanhMucCon == maDanhMuc &&
                    hs.Deleted == 0);

                foreach (TINRAOVAT trv in query)
                {
                    ketQua3.Add(ChuyenDTO(trv));
                    ketQua.Add(ChuyenDTO(trv));
                }

                if (count == 0)
                {
                    count = ketQua3.Count;
                }
            }

            if (diadiem.CompareTo("......") != 0)
            {
                int maDiaDiem = DiaDiemDAO.getMa(diadiem);

                SoTieuChiTraCuu++;
                var query = dbRaoVat.TINRAOVATs.Where(hs => hs.MaDiaDiem == maDiaDiem &&
                    hs.Deleted == 0);

                foreach (TINRAOVAT trv in query)
                {
                    ketQua4.Add(ChuyenDTO(trv));
                    ketQua.Add(ChuyenDTO(trv));
                }

                if (count == 0)
                {
                    count = ketQua4.Count;
                }
            }

            //Giá
            SoTieuChiTraCuu++;
            var truyvan = dbRaoVat.TINRAOVATs.Where(hs => hs.GiaTien >= giatu && hs.GiaTien <= giaden &&
                hs.Deleted == 0);

            foreach (TINRAOVAT trv in truyvan)
            {
                ketQua5.Add(ChuyenDTO(trv));
                ketQua.Add(ChuyenDTO(trv));
            }

            if (count == 0)
            {
                count = ketQua5.Count;
            }

            //Thời gian
            SoTieuChiTraCuu++;
            var truyvan1 = dbRaoVat.TINRAOVATs.Where(hs => hs.ThoiGianDang >= thoigiantu &&
                hs.ThoiGianDang <= thoigianden && hs.Deleted == 0);

            foreach (TINRAOVAT trv in truyvan1)
            {
                ketQua6.Add(ChuyenDTO(trv));
                ketQua.Add(ChuyenDTO(trv));
            }

            if (count == 0)
            {
                count = ketQua6.Count;
            }

            /////
            List<TinRaoVatDTO> kq = new List<TinRaoVatDTO>();
            for (int i = 0; i < count; ++i)
            {
                for (int j = 0; j < ketQua.Count; ++j)
                {
                    if (ketQua[i].MaTinRaoVat == ketQua[j].MaTinRaoVat)
                    {
                        dem++;
                    }

                    if (dem == SoTieuChiTraCuu && j == ketQua.Count - 1)
                    {
                        kq.Add(ketQua[i]);
                        dem = 0;
                        break;
                    }
                }

                dem = 0;
            }

            return kq;
        }

        private static bool KiemTraTonTai(List<TinRaoVatDTO> DS, TinRaoVatDTO tinraovat)
        {
            for (int i = 0; i < DS.Count; ++i)
            {
                if (DS[i].Equals(tinraovat))
                {
                    return true;
                }
            }

            return false;
        }

        public static int getMaDanhMucCon(int maTinRaoVat)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();
            var query = dbRaoVat.TINRAOVATs.Where(dm =>
                dm.MaTinRaoVat == maTinRaoVat).Single();
            return query.MaDanhMucCon.Value;
        }

        public static bool capNhatThoiGianUp(int maTinRaoVat, DateTime thoiGianUp)
        {
            bool result = true;
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TINRAOVAT _update = (from n in dbRaoVat.TINRAOVATs
                                 where n.MaTinRaoVat == maTinRaoVat
                                 select n).Single();

            _update.ThoiGianUp = thoiGianUp;

            try
            {
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
    }
}
